package com.tencent.sr.iris.baisc.saas.export.sdk;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.rmall.common.exception.TMallBizException;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.material.interfaces.client.TaskRecordClient;
import com.tencent.sr.rmall.material.interfaces.enums.ETaskBusinessType;
import com.tencent.sr.rmall.material.interfaces.enums.ETaskStatus;
import com.tencent.sr.rmall.material.interfaces.enums.ETaskType;
import com.tencent.sr.rmall.material.interfaces.request.AddTaskRecordRequest;
import com.tencent.sr.rmall.material.interfaces.request.PaginationRequest;
import com.tencent.sr.rmall.material.interfaces.request.SearchTaskRequest;
import com.tencent.sr.rmall.material.interfaces.request.UpdateTaskRecord;
import com.tencent.sr.rmall.material.interfaces.response.TaskRecordResponse;
import com.tencent.sr.rmall.material.interfaces.vo.PaginationData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TaskDependency {
    @Autowired
    private TaskRecordClient taskRecordClient;

    public TaskDependency() {
    }

    public Long addTask(AddTaskRecordRequest request) {
        HttpResult<Long> result = this.taskRecordClient.addTask(request);
        return result != null && result.isSuccess() ? (Long)result.getData() : null;
    }

    public PaginationData<TaskRecordResponse> getTaskList(PaginationRequest<SearchTaskRequest> request) {
        HttpResult<PaginationData<TaskRecordResponse>> result = this.taskRecordClient.getTaskList(request);
        return result != null && result.isSuccess() ? (PaginationData)result.getData() : null;
    }

    public Object updateTask(UpdateTaskRecord request) {
        HttpResult result = this.taskRecordClient.updateTask(request);
        return result != null && result.isSuccess() ? result.getData() : null;
    }

    public void updateTaskStatus(Long taskId, ETaskStatus handleSuccess, String url) {
        UpdateTaskRecord record = new UpdateTaskRecord();
        record.setTaskId(taskId.toString());
        record.setTaskStatus(handleSuccess.getStatus());
        record.setClientDownloadUrl(url);
        HttpResult httpResult = this.taskRecordClient.updateTask(record);
        log.info("更新任务 request={} result={}", record, httpResult);
        if (!httpResult.isSuccess()) {
            throw new TMallBizException("更新下载中心任务状态失败 msg=" + httpResult.getErrorMsg());
        }
    }

    public PaginationData<TaskRecordResponse> getTask(ETaskStatus taskStatus, ETaskBusinessType type) {
        SearchTaskRequest search = new SearchTaskRequest();
        search.setTaskStatus(taskStatus.getStatus());
        search.setTaskType(ETaskType.EXPORT.getType());
        if (type != null) {
            search.setBusinessType(type.getStatus());
        }

        search.setSortByCreateTimeAsc(true);
        PaginationRequest<SearchTaskRequest> page = new PaginationRequest();
        page.setPageSize(1);
        page.setPageNum(1);
        page.setQueryParameter(search);
        HttpResult<PaginationData<TaskRecordResponse>> result = this.taskRecordClient.getTaskList(page);
        if (result.isSuccess() && result.getData() != null) {
            log.info("getTask ,taskStatus:{},type:{} result:{}", new Object[]{JSON.toJSONString(taskStatus), JSON.toJSONString(type), JSON.toJSONString(result)});
            return (PaginationData)result.getData();
        } else {
            throw new TMallBizException("查询下载中心任务失败 msg=" + result.getErrorMsg());
        }
    }
}
